<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
	<?php
		require 'menus.php';
	?>
	<?php
		// Create the member profile
		function getMemberInfo()
		{
			$db = new Connect();
			
			$sql = "SELECT mi.mName, mi.email, mi.mCity, mp.mName as showName, mp.email as showEmail, mp.mCity as showCity " . 
					"FROM MemberInfo mi JOIN MemberPrivacy mp on mi.mID = mp.mID WHERE mi.mID = " . $_GET["mid"];
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find member
			{
				return;
			}
			else // Return member info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}

		function getPostings() 
		{
			$db = new Connect();
			
			// TODO: Use the real table name and columns
			$sql = "SELECT pID, pName, price, status " .
				"FROM Posting " .
				"WHERE mid = " . $_GET['mid'];
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find posting
			{
				return;
			}
			else // Return posting info
			{
				$rows = $db->to_array($result);
				return $rows;
			}
		}
	?>
    	<div id="main">	
		<p class="title">Member Profile</p>
		
		<?php
			$info = getMemberInfo();
	
			$name = ($info['showName']) ? $info['mName'] : 'Anonymous' ;
			$email = ($info['showEmail']) ? $info['email'] : 'private' ;
			$city = ($info['showCity']) ? $info['mCity'] : 'private' ;
			
			print "<p class=\"normal\">Member ID: " . $_GET['mid'] . "</p>";
			print "<p class=\"normal\">Name: " . $name . "</p>";
			print "<p class=\"normal\">Email: " . $email . "</p>";
			print "<p class=\"normal\">Location: " . $city . "</p>";
			print "<p></p>";
			print "<p></p>";
			print "<p></p>";
			print "<p class=\"normal\">Click on the post's number link to go to the selected page.</p>";
			echo '<form id="member" method="post">';
	
			$rows = getPostings();
		
			if (!isset($rows))
			{
				echo "No postings found.";
			}
			else
			{
				echo "<table border=1>";
				echo "<tr>";
				echo "<th>ID</th>";
				echo "<th>Name</th>";
				echo "<th>Status</th>";
				echo "<th>Price</th>";
				echo "</tr>";

				$rowcount = count($rows);
				for ($i = 0; $i < $rowcount; $i++)
				{			
					echo "<tr>";
					echo '<td><a href="posting.php?pid='.$rows[$i]['pID'].'"> '. $rows[$i]['pID'] .'</a></td>';
					echo '<td>'. $rows[$i]['pName'] .'</td>';
					echo '<td>'. $rows[$i]['status'] .'</td>';
					echo '<td>'. $rows[$i]['price'] .'</td>';
					echo "</tr>";
				}
			}
			echo "</table>";		
			echo '</form>';
		?>
	</div>
</body>
</html>
